<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pseudorandom_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_pseudorandom_test.js.html">pseudorandom_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.PseudoRandomTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.PseudoRandomTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.testing.PseudoRandom&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>function runFairnessTest(sides, rolls, chiSquareLimit) {
<a name="line22"></a>  // Initialize the count table for dice rolls.
<a name="line23"></a>  var counts = [];
<a name="line24"></a>  for (var i = 0; i &lt; sides; ++i) {
<a name="line25"></a>    counts[i] = 0;
<a name="line26"></a>  }
<a name="line27"></a>  // Roll the dice many times and count the results.
<a name="line28"></a>  for (var i = 0; i &lt; rolls; ++i) {
<a name="line29"></a>    ++counts[Math.floor(Math.random() * sides)];
<a name="line30"></a>  }
<a name="line31"></a>  // If the dice is fair, we expect a uniform distribution.
<a name="line32"></a>  var expected = rolls / sides;
<a name="line33"></a>  // Pearson&#39;s chi-square test for a distribution fit.
<a name="line34"></a>  var chiSquare = 0;
<a name="line35"></a>  for (var i = 0; i &lt; sides; ++i) {
<a name="line36"></a>    chiSquare += (counts[i] - expected) * (counts[i] - expected) / expected;
<a name="line37"></a>  }
<a name="line38"></a>  assert(&#39;Chi-square test for a distribution fit failed&#39;,
<a name="line39"></a>      chiSquare &lt; chiSquareLimit);
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function testInstall() {
<a name="line43"></a>  var random = new goog.testing.PseudoRandom();
<a name="line44"></a>  var originalRandom = Math.random;
<a name="line45"></a>
<a name="line46"></a>  assertFalse(!!random.installed_);
<a name="line47"></a>
<a name="line48"></a>  random.install();
<a name="line49"></a>  assertTrue(random.installed_);
<a name="line50"></a>  assertNotEquals(Math.random, originalRandom);
<a name="line51"></a>
<a name="line52"></a>  random.uninstall();
<a name="line53"></a>  assertFalse(random.installed_);
<a name="line54"></a>  assertEquals(originalRandom, Math.random);
<a name="line55"></a>}
<a name="line56"></a>
<a name="line57"></a>function testBounds() {
<a name="line58"></a>  var random = new goog.testing.PseudoRandom();
<a name="line59"></a>  random.install();
<a name="line60"></a>
<a name="line61"></a>  for (var i = 0; i &lt; 100000; ++i) {
<a name="line62"></a>    var value = Math.random();
<a name="line63"></a>    assert(&#39;Random value out of bounds&#39;, value &gt;= 0 &amp;&amp; value &lt; 1);
<a name="line64"></a>  }
<a name="line65"></a>
<a name="line66"></a>  random.uninstall();
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function testFairness() {
<a name="line70"></a>  var random = new goog.testing.PseudoRandom(0, true);
<a name="line71"></a>
<a name="line72"></a>  // Chi-square statistics: p-value = 0.05, df = 5, limit = 11.07.
<a name="line73"></a>  runFairnessTest(6, 100000, 11.07);
<a name="line74"></a>  // Chi-square statistics: p-value = 0.05, df = 100, limit = 124.34.
<a name="line75"></a>  runFairnessTest(101, 100000, 124.34);
<a name="line76"></a>
<a name="line77"></a>  random.uninstall();
<a name="line78"></a>}
<a name="line79"></a>
<a name="line80"></a>function testReseed() {
<a name="line81"></a>  var random = new goog.testing.PseudoRandom(100, true);
<a name="line82"></a>
<a name="line83"></a>  var sequence = [];
<a name="line84"></a>  for (var i = 0; i &lt; 64000; ++i) {
<a name="line85"></a>    sequence.push(Math.random());
<a name="line86"></a>  }
<a name="line87"></a>
<a name="line88"></a>  random.seed(100);
<a name="line89"></a>  for (var i = 0; i &lt; sequence.length; ++i) {
<a name="line90"></a>    assertEquals(sequence[i], Math.random());
<a name="line91"></a>  }
<a name="line92"></a>
<a name="line93"></a>  random.uninstall();
<a name="line94"></a>}
</pre>


</body>
</html>
